package com.magicsoft.app.wcf;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.boyuan.aboutcase.CaseNew;
import com.boyuan.aboutcase.Caselist;
import com.boyuan.huijiaoyu.AddGroup;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.magicsoft.app.entity.CaseDetailEntity;
import com.magicsoft.app.helper.TokenHelper;
import com.magicsoft.app.wcf.listener.GetMultilParamsListener;
import com.magicsoft.app.wcf.listener.GetOneRecordListener;
import com.magicsoft.app.wcf.listener.PostRecordResponseListener;
import com.magicsoft.constant.Constant;

import android.content.Context;
import android.util.Log;

public class CaseService extends BaseService {
	private String tag = "CaseService";

	public CaseService(Context context) {
		super(context);
	}

	/*
	 * 修改Case详情信息
	 */
	public void ChangeCaseDetail(String corp_id, String cid, String title,
			String desc, String type_id,
			String priority, String status, String operator_id,
			String operator, final PostRecordResponseListener listener) {
		RequestParams requestParams = new RequestParams();
		try {
			requestParams = TokenHelper.FetchChangeCaseDetail(context, true,
					corp_id, cid, title, desc, type_id,
					priority, status, operator_id, operator);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String requestUrl = Constant.CASES + "?";
		AsyncHttpServiceHelper.put(requestUrl, requestParams,
				new JsonHttpResponseHandler() {
					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						super.onSuccess(statusCode, headers, response);
						try {
							String message = response.getString("message");
							String code = response.getString("code");
							if ("0".equals(code)) {
								if (listener != null) {
									listener.onFinish(message);
								}
							} else {
								if (listener != null) {
									listener.onFailed(message);
								}
							}
						} catch (JSONException e) {
							if (listener != null) {
								listener.onFailed(e.getMessage());
							}
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						Log.e("HomeService ChangeCaseDetail",
								"onFailure responseString=" + responseString);
						super.onFailure(statusCode, headers, responseString,
								throwable);
					}
				});
	}

	/*
	 * 删除动态
	 */
	public void deleteCaseTrend(String corp_id, String trend_id,
			String operator_id, String operator,
			final PostRecordResponseListener listener) {
		String getParamString = "";
		try {
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String requestUrl = Constant.TRENDS + getParamString + "&corp_id="
				+ corp_id + "&trend_id=" + trend_id + "&operator_id="
				+ operator_id + "&operator=" + operator;
		AsyncHttpServiceHelper.delete(requestUrl,
				new JsonHttpResponseHandler() {
					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						try {
							String code = response.getString("code");
							String message = response.getString("message");
							if ("0".equals(code)) {
								if (listener != null) {
									listener.onFinish(message);
								}
							} else {
								if (listener != null) {
									listener.onFailed(message);
								}
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

						super.onSuccess(statusCode, headers, response);
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						super.onFailure(statusCode, headers, responseString,
								throwable);
					}
				});
	}

	/*
	 * 添加动态与评论
	 */
	public void addTrends(String corp_id, String case_id, String content,
			String operator_id, String operator,
			final PostRecordResponseListener listener) {
		RequestParams requestParams = new RequestParams();
		try {
			requestParams = TokenHelper.FetchAddTrends(context, true, corp_id,
					case_id, content, operator_id, operator);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String requestUrl = Constant.TRENDS;
		//
		// Log.e(tag,
		// "addTrends requestUrl = " + requestUrl
		// + requestParams.toString());
		AsyncHttpServiceHelper.post(requestUrl, requestParams,
				new JsonHttpResponseHandler() {
					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						super.onSuccess(statusCode, headers, response);
						try {
							String code = response.getString("code");
							String message = response.getString("message");
							if ("0".equals(code)) {
								if (listener != null) {
									listener.onFinish(message);
								}
							} else {
								if (listener != null) {
									listener.onFailed(message);
								}

							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						super.onFailure(statusCode, headers, responseString,
								throwable);
					}
				});
	}

	/*
	 * 添加Case
	 */
	public void addCase(String title, String desc, String priority, String operator_id, String operator, final GetMultilParamsListener listener) {
		RequestParams params = new RequestParams();

		try {
			params = TokenHelper.FetchAddCase(context, true, title, desc, priority, operator_id, operator);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String requestUrl = Constant.CASESNEW;
		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {
					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						super.onSuccess(statusCode, headers, response);
						try {
							String code = response.getString("code");
							String message = response.getString("message");
							if ("0".equals(code)) {
								JSONObject content = response.getJSONObject("content");
								Type contentType = new TypeToken<CaseNew>() {}.getType();
								CaseNew caseNew = gson.fromJson(
										content.toString(), contentType);
								if (listener != null) {
									listener.onFinish(caseNew);
								}
							} else {
								if (listener != null) {
									listener.onFailed(message);
								}

							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						if (listener != null) {
							listener.onFailed("");
						}
						super.onFailure(statusCode, headers, responseString,
								throwable);
					}
					
					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONArray errorResponse) {
						if (listener != null) {
							listener.onFailed("");
						}
						super.onFailure(statusCode, headers, throwable, errorResponse);
					}
					
					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						if (listener != null) {
							listener.onFailed("");
						}
						super.onFailure(statusCode, headers, throwable, errorResponse);
					}
				});
	}
	

	// 取消关注
	public void CancelAttention(String corp_id, String case_id,
			String operator_id, String operator,
			final PostRecordResponseListener listener) {
		String getParamString = "";
		try {
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String requestUrl = Constant.CASES_MEMBERS + getParamString
				+ "&corp_id=" + corp_id + "&case_id=" + case_id
				+ "&operator_id=" + operator_id + "&operator=" + operator;
		AsyncHttpServiceHelper.delete(requestUrl,
				new JsonHttpResponseHandler() {
					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						Log.e("response", response.toString());
						super.onSuccess(statusCode, headers, response);
						try {
							String code = response.getString("code");
							String message = response.getString("message");
							if ("0".equals(code)) {
								if (listener != null) {
									listener.onFinish(message);
								}
							} else {
								if (listener != null) {
									listener.onFailed(message);
								}
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
					}
				});
	}

	/*
	 * 添加关注人
	 */

	public void addCasesMembers(String corp_id, String case_id, String members,
			String operator_id, String operator,
			final PostRecordResponseListener listener) {
		RequestParams requestParams = new RequestParams();
		try {
			requestParams = TokenHelper.FetchAddCaseMembers(context, true,
					corp_id, case_id, members, operator_id, operator);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String requestUrl = Constant.CASES_MEMBERS;
		AsyncHttpServiceHelper.post(requestUrl, requestParams,
				new JsonHttpResponseHandler() {
					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						super.onSuccess(statusCode, headers, response);
						try {
							String code = response.getString("code");
							String message = response.getString("message");
							if ("0".equals(code)) {
								if (listener != null) {
									listener.onFinish(message);
								}
							} else {
								if (listener != null) {
									listener.onFailed(message);
								}
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						if (listener != null) {
							listener.onFailed("");
						}
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
					}
				});
	}

	// 上传附件
	public void CaseUpLoad(String corp_id, String case_id, String file_name,

	String operator_id, String operator) {
		RequestParams requestParams = new RequestParams();
		try {
			requestParams = TokenHelper.FetchUpLoadCaseFiles(context, true,
					corp_id, case_id, file_name, operator_id, operator);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		String requestUrl = Constant.FILES;
		AsyncHttpServiceHelper.post(requestUrl, requestParams,
				new JsonHttpResponseHandler() {
					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						super.onSuccess(statusCode, headers, response);
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
					}
				});
	}

	/*
	 * 获取Case类型
	 */

	public void getCaseType(String corp_id,
			final PostRecordResponseListener listener) {
		String getParamString = "";
		try {
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String requestUrl = Constant.CASE_TYPE + getParamString + "&corp_id=1";

		AsyncHttpServiceHelper.get(requestUrl, new JsonHttpResponseHandler() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				try {
					String code = response.getString("code");
					if ("0".equals(code)) {
						JSONArray contentArray = response
								.getJSONArray("content");
						JSONObject jsonObject = (JSONObject) contentArray
								.get(0);
						String id = jsonObject.getString("id");
						if (listener != null) {
							listener.onFinish(id);
						}
					} else {
						String message = response.getString("message");
						if (listener != null) {
							listener.onFailed(message);
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				super.onSuccess(statusCode, headers, response);
			}

		});

	}
	
	//获取未关闭的case列表
	public void getUnclsecase(String eid, String pagenum, final GetMultilParamsListener listener){
		String getParamString = "";
		try {
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String requestUrl = Constant.CASE_LIST + getParamString + "&corp_id=1&list_type=unclosed&eid=" + eid + "&page_num="+ pagenum +"&page_size=10";
		AsyncHttpServiceHelper.get(requestUrl, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				try {
					String code = response.getString("code");
					String message = response.getString("message");
					if("0".equals(code)){
						JSONObject content = response.getJSONObject("content");
						Type contentType = new TypeToken<Caselist>() {}.getType();
						Caselist caselist = gson.fromJson(
								content.toString(), contentType);
						if (listener != null) {
							listener.onFinish(caselist);
						}
					}else{
						if (listener != null) {
							listener.onFailed(message);
						}
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				super.onSuccess(statusCode, headers, response);
			}
		});
	}
	
	//搜索未关闭的case列表
		public void getSearchUnclsecase(String eid, String pagenum, String keyword, final GetMultilParamsListener listener){
			String getParamString = "";
			try {
				getParamString = TokenHelper.FetchColourToken(context, false);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String requestUrl = Constant.CASE_LIST + getParamString + "&corp_id=1&list_type=unclosed&eid=" + eid +"&keyword=" + keyword + "&page_num="+ pagenum +"&page_size=10";
			AsyncHttpServiceHelper.get(requestUrl, new JsonHttpResponseHandler(){
				@Override
				public void onSuccess(int statusCode, Header[] headers,
						JSONObject response) {
					try {
						String code = response.getString("code");
						String message = response.getString("message");
						if("0".equals(code)){
							JSONObject content = response.getJSONObject("content");
							Type contentType = new TypeToken<Caselist>() {}.getType();
							Caselist caselist = gson.fromJson(
									content.toString(), contentType);
							if (listener != null) {
								listener.onFinish(caselist);
							}
						}else{
							if (listener != null) {
								listener.onFailed(message);
							}
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					super.onSuccess(statusCode, headers, response);
				}
			});
		}

	public void getClsecase(String eid, String pagenum, final GetMultilParamsListener listener) {
		String getParamString = "";
		try {
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String requestUrl = Constant.CASE_LIST + getParamString + "&corp_id=1&list_type=closed&eid=" + eid + "&page_num="+ pagenum + "&page_size=10";
		AsyncHttpServiceHelper.get(requestUrl, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				try {
					String code = response.getString("code");
					String message = response.getString("message");
					if("0".equals(code)){
						JSONObject content = response.getJSONObject("content");
						Type contentType = new TypeToken<Caselist>() {}.getType();
						Caselist caselist = gson.fromJson(
								content.toString(), contentType);
						if (listener != null) {
							listener.onFinish(caselist);
						}
					}else{
						if (listener != null) {
							listener.onFailed(message);
						}
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				super.onSuccess(statusCode, headers, response);
			}
		});
	}
	
	public void getSearchClsecase(String eid, String pagenum, String keyword, final GetMultilParamsListener listener) {
		String getParamString = "";
		try {
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String requestUrl = Constant.CASE_LIST + getParamString + "&corp_id=1&list_type=closed&eid=" + eid + "&keyword=" + keyword + "&page_num="+ pagenum + "&page_size=10";
		AsyncHttpServiceHelper.get(requestUrl, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				try {
					String code = response.getString("code");
					String message = response.getString("message");
					if("0".equals(code)){
						JSONObject content = response.getJSONObject("content");
						Type contentType = new TypeToken<Caselist>() {}.getType();
						Caselist caselist = gson.fromJson(
								content.toString(), contentType);
						if (listener != null) {
							listener.onFinish(caselist);
						}
					}else{
						if (listener != null) {
							listener.onFailed(message);
						}
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				super.onSuccess(statusCode, headers, response);
			}
		});
	}
	
	//下载附件
	
	
	//删除附件
	public void deleteFile(String corp_id, String file_id, String case_id, String operator_id, String operator, final PostRecordResponseListener listener){
		String getParamString = "";
		try {
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String requestUrl = Constant.FILES + getParamString + "&corp_id="
				+ corp_id + "&case_id=" + case_id + "&file_id=" + file_id
				+ "&operator_id=" + operator_id + "&operator=" + operator;
		AsyncHttpServiceHelper.delete(requestUrl,
				new JsonHttpResponseHandler() {
					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						try {
							String code = response.getString("code");
							String message = response.getString("message");
							if("0".equals(code)){
								if (listener != null) {
									listener.onFinish(message);
								}
							}else{
								if (listener != null) {
									Log.e("13246", message);
									listener.onFinish(message);
								}
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						super.onSuccess(statusCode, headers, response);
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						Log.e("删除失败1", errorResponse.toString());
						if (listener != null) {
							listener.onFinish("删除失败1");
						}
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
					}
					
					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						Log.e("删除失败2", responseString);
						super.onFailure(statusCode, headers, responseString, throwable);
					}
				});
	}
}
